package com.mywebbox.mycraftplugin;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerInteractEvent;

public class MyCraftPluginEvents implements Listener {

	private MyCraftPlugin plugin;

	public MyCraftPluginEvents(MyCraftPlugin plugin) {
		this.plugin = plugin;
	}

	@EventHandler
	public void onOpenCoffre(InventoryOpenEvent evt) {
		// Player p = (Player)evt.getPlayer();
		/*
		 * if(evt.getInventory().getType().name().equals("CHEST")) {
		 * evt.getInventory() evt.setCancelled(true);
		 * 
		 * }
		 */

	}

	@EventHandler
	public void onClickCoffre(PlayerInteractEvent evt) {
		if (evt.getAction() == Action.RIGHT_CLICK_BLOCK) {
			Block b = evt.getClickedBlock();
			Location l = b.getLocation();
			Location l2 = l.clone();
			l2.setX(l2.getX() + 1);
			Block b2 = l2.getBlock();
			if (b.getType().equals(Material.CHEST)
					&& b2.getType().equals(Material.SIGN_POST)) {
				Sign s = (Sign) b2.getState();
				String[] text = s.getLines();
				boolean a = false;
				if (text[0].equalsIgnoreCase("[private]")) {
					for (int i = 1; i < text.length; i++) {
						if (plugin.getServer().getPlayer(text[i]) != null) {
							a = true;
						}
					}
					if (a) {
						boolean c = true;
						for (int i = 1; i < text.length; i++) {
							if (evt.getPlayer().getName().equals(text[i])) {
								c = false;
							}

							evt.setCancelled(c);
						}
					}

				}

			}
		}
	}

}
